package com.hhc.repository;

import com.hhc.entity.PermissionInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 权限信息数据访问层接口
 *
 * @author pc
 */
@Repository
public interface PermissionInfoRepository extends JpaRepository<PermissionInfo, Long> {

    /**
     * 检查指定权限名称是否存在
     *
     * @param permissionName 权限名称
     * @return 是否存在
     */
    boolean existsByPermissionName(String permissionName);

    @Query("SELECT r.roleName " +
            "FROM UserRoleRelation ur " +
            "JOIN RoleInfo r ON ur.roleId = r.id " +
            "WHERE ur.userId = :userId")
    List<String> findUserRoles(@Param("userId") Long userId);

    @Query("SELECT p.permissionName " +
            "FROM UserRoleRelation ur " +
            "JOIN RolePermissionRelation rp ON ur.roleId = rp.roleId " +
            "JOIN PermissionInfo p ON rp.permissionId = p.id " +
            "WHERE ur.userId = :userId")
    List<String> findUserPermissions(@Param("userId") Long userId);
}